﻿using System;

namespace CSharpConsoleTestSolution.Demo {
    //表达式
    public class P15_Expression {
        /**
         * 块语句里的 goto: 时间: 07:20
         */
        public static void goto_exam() {
            int score = 101;
            hello:
            Console.WriteLine($"Hello World!, score = {score}");
            if (--score > 0) goto hello;
        }


        /**
         * switch 里的 goto: 时间: 58:50
         */
        public static void switch_goto() {
            Console.WriteLine("请输入分数(0~100):");
            string str = Console.ReadLine();
            int score = int.Parse(str);

            switch (score % 10) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                    Console.WriteLine("来到了case 7");
                    break;
                case 8:
                    Console.WriteLine("来到了case 8");
                    break;
                case 9:
                    if (score > 100) {
                        goto default; //goto default
                    } else {
                        goto case 8; //goto case 8
                    }
                default:
                    Console.WriteLine("default: 请输入正确的分数哦!");
                    switch_goto();
                    break;
            }
        }


        /**
         * catch 无参, 时间: 1:16:10
         */
        public static void try_catch() {
            Console.WriteLine("请输入分数(0~100):");
            string str = Console.ReadLine();
            try {
                int score = int.Parse(str);
                Console.WriteLine($"score = {score}");
            } catch {
                Console.WriteLine("请输入正确的分数!");
            }
        }


        /**
         * catch 多种类型, finally, throw, 时间: 1:17:36
         */
        public static void try_catch2() {
            Console.WriteLine("请输入分数(0~100):");
            string str = Console.ReadLine();
            try {
                int score = int.Parse(str);
                Console.WriteLine($"score = {score}");
            }
            catch (ArgumentNullException e) { //catch 1
                Console.WriteLine(e);
            }
            catch (FormatException e) {     //catch 2
                Console.WriteLine(e);
            }
            catch (OverflowException e) {   //catch 3
                Console.WriteLine(e);
                throw e;                //throw 将异常丢出去
            }
            finally {
                Console.WriteLine("finally 最终都会执行!");
            }
        }
    }
}